#include "stdafx.h"
#include "LoadTestFunctions.h"

/*static*/HMODULE CFFmpegTestFunctions::hModule = NULL;

/*static*/int CFFmpegTestFunctions::dct_test(int argc, char **argv)
{
    typedef int(__cdecl * DCT_TEST_FUNC)(int argc, char **argv);

    DCT_TEST_FUNC dct_test = (DCT_TEST_FUNC)CFFmpegTestFunctions::GetFunctionAddress("dct_test");
    if (dct_test==NULL)
    {
        throw std::exception("Could not load DLL.");
    }

    int r = (dct_test)(argc,argv); 
    return r;
}

/*static*/int CFFmpegTestFunctions::fft_test(int argc, char **argv)
{
    typedef int(__cdecl * FFT_TEST_FUNC)(int argc, char **argv);

    FFT_TEST_FUNC fft_test = (FFT_TEST_FUNC)CFFmpegTestFunctions::GetFunctionAddress("fft_test");
    if (fft_test==NULL)
    {
        throw std::exception("Could not load DLL.");
    }

    int r = (fft_test)(argc,argv); 
    return r;
}

/*static*/int CFFmpegTestFunctions::fft_fixed_test(int argc, char **argv)
{
    typedef int(__cdecl * FFT_TEST_FUNC)(int argc, char **argv);

    FFT_TEST_FUNC fft_test = (FFT_TEST_FUNC)CFFmpegTestFunctions::GetFunctionAddress("fft_fixed_test");
    if (fft_test==NULL)
    {
        throw std::exception("Could not load DLL.");
    }

    int r = (fft_test)(argc,argv); 
    return r;
}

/*static*/bool CFFmpegTestFunctions::EnsureDllIsLoaded(LPCTSTR dllName)
{
    if (CFFmpegTestFunctions::hModule!=NULL)
    {
        return true;
    }

    CFFmpegTestFunctions::hModule = LoadLibrary(dllName);
    return CFFmpegTestFunctions::hModule!=NULL?true:false;
}

/*static*/FARPROC CFFmpegTestFunctions::GetFunctionAddress(LPSTR funcName)
{
    static LPCTSTR dllName = TEXT(".\\winffmpeg.dll");
    
    if (EnsureDllIsLoaded(dllName)!=true)
    {
        return NULL;
    }

    FARPROC fp = GetProcAddress(CFFmpegTestFunctions::hModule,funcName);
    return fp;
}
